package cn.com.qiter.staservice.service.impl;

import cn.com.qiter.common.AppConstant;
import cn.com.qiter.common.Result;
import cn.com.qiter.common.vo.ProjectModel;
import cn.com.qiter.staservice.client.TrafficClient;
import cn.com.qiter.staservice.client.UcenterClient;
import cn.com.qiter.staservice.entity.StatisticsWorkdaily;
import cn.com.qiter.staservice.mapper.StatisticsWorkdailyMapper;
import cn.com.qiter.staservice.service.StatisticsWorkdailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.java.Log;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * <p>
 * 统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-19
 */
@Service
@Log4j2
public class StatisticsWorkdailyServiceImpl extends ServiceImpl<StatisticsWorkdailyMapper, StatisticsWorkdaily> implements StatisticsWorkdailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Autowired
    private TrafficClient trafficClient;

    @Override
    public void trapCount(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        wrapper.eq("work_type",AppConstant.TRAP);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = ucenterClient.countTrapDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.TRAP);
        baseMapper.insert(sta);
    }

    @Override
    public void trafficCount(String day) {
        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        wrapper.eq("work_type",AppConstant.TRAFFIC);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = trafficClient.countTrafficDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.TRAFFIC);
        baseMapper.insert(sta);
    }

    @Override
    public void workCount(String day) {

        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        wrapper.eq("work_type",AppConstant.PESTS);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = ucenterClient.countPestsDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.PESTS);
        baseMapper.insert(sta);
    }

    @Override
    public void otherCount(String day) {

        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        wrapper.eq("work_type",AppConstant.OTHER);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = trafficClient.countTrafficDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.OTHER);
        baseMapper.insert(sta);
    }

    @Override
    public void santitaionCount(String day) {

        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        wrapper.eq("work_type",AppConstant.SANTITATION);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = trafficClient.countTrafficDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.SANTITATION);
        baseMapper.insert(sta);
    }
    @Override
    public void videoCount(String day) {

        //添加记录之前删除表相同日期的数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到某一天注册人数
        Result registerR = trafficClient.countVideoDay(day);
        Integer countDay = (Integer)registerR.getData().get("countDay");

        //把获取数据添加数据库，统计分析表里面
        StatisticsWorkdaily sta = new StatisticsWorkdaily();
        sta.setWorkNum(countDay); //数量
        sta.setDateCalculated(day);//统计日期

        sta.setWorkType(AppConstant.VIDEO);
        baseMapper.insert(sta);
    }
    @Override
    public Result getShowData(String type, String begin, String end) {
        //根据条件查询对应数据
        QueryWrapper<StatisticsWorkdaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.eq("work_type",type);
        List<StatisticsWorkdaily> staList = this.list(wrapper);
        return Result.ok().data("data",staList);
    }

    @Override
    public Integer countTrap() {
        Integer count = ucenterClient.countTrapAll();
        return count;
    }

    @Override
    public Integer countPests() {
        Integer count = ucenterClient.countPestsAll();
        return count;
    }

    @Override
    public Integer countPestsAndTrap() {
        Result result = ucenterClient.getProjectAll();
        ArrayList list = (ArrayList) result.getData().get("items");
        log.error(list);
        return 1;
    }
}
